package com.conn;

import com.conn.*;

import java.io.*;

import javax.xml.parsers.*;
import org.w3c.dom.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.xml.sax.*;

class XmlHeader1 {
    public String name;
    public String ver;
    public String id;
}

class XmlHelper1 {
    public static Document genDocWithHeader(Command cmdid) {
        Document doc = null;
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.newDocument();
            Element root = doc.createElement("simpletalk");
            doc.appendChild(root);
            Element version = doc.createElement("version");
            Text verValue = doc.createTextNode("1.0");
            version.appendChild(verValue);
            root.appendChild(version);

            Element id = doc.createElement("id");
            Text idValue = doc.createTextNode(cmdid.getId());
            id.appendChild(idValue);

            doc.getElementsByTagName("simpletalk").item(0).appendChild(id);
        } catch (ParserConfigurationException ex) {
            ex.printStackTrace();
        }
        return doc;
    }

    public static Document genDocFromStream(byte[] bytesBuf, int len) {
        //XmlHeader1 h = new XmlHeader1();
        try {
//            new FileOutputStream("aa.xml").write(bytesBuf);
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document xmlDoc = db.parse(new InputSource(new ByteArrayInputStream(bytesBuf, 0, len)));
            return xmlDoc;
        } catch (ParserConfigurationException ex) {
            ex.printStackTrace();
        } catch (SAXException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static String getItemValue(Document xmlDoc, String itemNmae, int index) {
        Element root = xmlDoc.getDocumentElement();
        //h.name = root.getTagName(); 
        //h.ver = root.getElementsByTagName("version").item(0).getFirstChild().getNodeValue();
        //h.id = root.getElementsByTagName("id").item(0).getFirstChild().getNodeValue();
        //System.out.println(h.name);
        //System.out.println(h.ver);
        //System.out.println(h.id);
        NodeList nl = root.getElementsByTagName(itemNmae);
        if (null != nl && null != nl.item(index)
                && null != nl.item(index).getFirstChild())
            return nl.item(index).getFirstChild().getNodeValue();
        else 
            return null;
    }

    public static void addElement(Document doc, String name, String value) {
        Element elem = doc.createElement(name);
        Text elemValue = doc.createTextNode(value);
        elem.appendChild(elemValue);
        NodeList nl = doc.getElementsByTagName("simpletalk");
        //if (null != nl && null != nl.item(0)) //
        nl.item(0).appendChild(elem);
    }

    public static String DoctoString(Document doc) {
        TransformerFactory transFactory=TransformerFactory.newInstance();
        try {
            Transformer transformer = transFactory.newTransformer();
            transformer.setOutputProperty("indent", "yes");
            DOMSource source =new DOMSource();
            source.setNode(doc);
            StreamResult result = new StreamResult();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            result.setOutputStream(baos);
            //result.setOutputStream(System.out);
            transformer.transform(source, result);
            return baos.toString();
        } catch (TransformerConfigurationException ex) {
            ex.printStackTrace();
        } catch (TransformerException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}

            //transformer.setOutputProperty("encoding","UTF-8");
//            try {
//                result.setOutputStream(new FileOutputStream("book.xml"));
//            } catch (FileNotFoundException ex) {
//            
//            }
            //result.setOutputStream(System.out);
